<template>
  <div class="list-pages white-bg">
    <div class="search-box">
      <a-select
        :bordered="false"
        class="search-select"
       v-model:value="type"
        allow-clear
        placeholder="全部"
        :options="options"
        @change="handleChange"
      />
      <div class="line"></div>
      <div class="search-input-box">
        <a-input
          v-model:value="searchText"
          allow-clear
          placeholder="请输入实验名称"
          :bordered="false"
        >
          <template #prefix>
            <SearchOutlined />
          </template>
        </a-input>
        <a-button type="primary" @click="getData()">搜索</a-button>
      </div>
    </div>
    <div class="list-scroll">
      <div class="list-box">
        <div
          class="list-item"
          v-for="(item, index) in listData"
          :key="index"
          @click="handlePreview(item)"
        >
          <!-- <a-tooltip
            title="已收藏"
            showArrow
            v-if="item.collet"
            placement="top"
          >
            <div class="item-collect cur"></div>
          </a-tooltip>
          <div class="item-collect" v-else></div> -->
          <div class="list-img">
            <img :src="item.thumb_url" alt="" />
          </div>
          <div class="list-content">
            <div class="item-tag">
              {{ item.instrument_category_name }}
            </div>
            <div class="content-title">{{ item.name }}</div>
          </div>
        </div>
      </div>
    </div>
    <div class="pagination-box">
      <a-pagination
        v-model:current="current"
        v-model:page-size="pageSize"
        :total="total"
        :showSizeChanger="true"
        :show-total="(total) => `共 ${total}条数据`"
        @showSizeChange="onPageSizeChange"
        @change="onCurrentChange"
      />
    </div>
  </div>
</template>
<script setup>
import { ref, onMounted } from "vue";
import { SearchOutlined } from "@ant-design/icons-vue";
import { useRouter } from "vue-router";
import { useGlobalStore } from "@/store/modules/global";
import API from "@/api/user";
const store = useGlobalStore();
const router = useRouter();
const type = ref(null);
const searchText = ref("");
const options = ref([]);
const listData = ref([]);

const onEnter = () => {};
const onChange = () => {};

const total = ref(0);
const current = ref(1);
const pageSize = ref(10);
const onPageSizeChange = (page, size) => {
  console.log("page-size:", size);
  pageSize.value = size;
  getData();
};

const onCurrentChange = (index, pageInfo) => {
  console.log(pageInfo, index);
  current.value = index;
  getData();
};
const handleChange = (value) => {
  if(value === undefined) {
    type.value = null
    getData()
    return
  }else{
    type.value = value
    getData()
  }

  console.log(111,value)
}
const getData = async () => {
  console.log(1111, searchText.value, type.value);
  let params = {
    current: current.value,
    pageSize: pageSize.value,
    keyword: searchText.value,
    instrument_category_idArray: type.value !== null ? [type.value] : null,
    enabled: true,
  };
  const res = await API.InstrumentPagedList({
    args: params,
    //type: type.value,
  });
  console.log(res);
  total.value = res.total;
  listData.value = res.data;
};
const getOptionsData = async () => {
  const res = await API.InstrumentCategoryOptions({});
  console.log(res);
  options.value = res;
};
onMounted(async () => {
  await getData();
  await getOptionsData();
});
const handlePreview = async(item) => {
  await API.InstrumentInstrumentClicked({
    instrument_id:item.instrument_id
  })
  router.push({
    path: "/encyclopedia-detail",
    query: {
      id: item.instrument_id,
    },
  });
};
</script>
<style lang="less" scoped>
.list-pages {
  height: 100%;
  padding: 0 16px;
  display: flex;
  flex-direction: column;
  .search-box {
    display: flex;
    align-items: center;
    padding: 18px 0;
    flex-wrap: wrap;
    border-bottom: 1px solid #e5e7eb;
    .search-select {
      width: 120px;
      @media screen and (max-width: 768px) {
        width: 100%;
      }
    }
    .line {
      width: 1px;
      height: 16px;
      margin: 0 12px;
      background: #bec4ca;
      @media screen and (max-width: 768px) {
        display: none;
      }
    }
    .search-input-box {
      flex: 1;
      display: flex;
      align-items: center;
      @media screen and (max-width: 768px) {
        width: 100%;
        margin-top: 16px;
        justify-content: space-between;
      }
    }
    .search-input {
      flex: 1;
    }
    .search-btn {
      width: 60px;
      height: 32px;
      background: #2776ff;
      border-radius: 4px;
      font-family: PingFangSC-Regular;
      font-weight: 400;
      font-size: 14px;
      color: #ffffff;
      text-align: center;
      line-height: 32px;
    }
  }
  .list-scroll {
    width: 100%;
    flex: 1;
  }
  .list-box {
    width: 100%;
    box-sizing: border-box;
    padding-top: 16px;
    overflow: auto;
    display: grid;
    gap: 16px;
    margin: 0 auto;
    grid-template-columns: repeat(auto-fill, minmax(261px, 1fr));
    justify-content: space-between;
    .list-item {
      width: 261px;
      background: #ffffff;
      border: 1px solid #e6ebf1;
      border-radius: 8px;
      margin: auto;
      box-sizing: border-box;
      padding: 8px;
      position: relative;
      cursor: pointer;
      &:hover {
        border: 1px solid #2776ff;
        box-shadow: 12px 12px 12px 0px rgba(39, 118, 255, 0.1);
      }
      .item-collect {
        position: absolute;
        top: 0;
        right: 0;
        cursor: pointer;
        width: 32px;
        height: 32px;
        background: rgba(0, 0, 0, 0.4) url(@/assets/icon-collet.png) no-repeat
          center center;
        border-radius: 0 8px 0 12px;
        &.cur {
          background: #ffffff url(@/assets/icon-collet-cur.png) no-repeat center
            center;
          border-radius: 0 8px 0 12px;
        }
      }

      .list-img {
        width: 245px;
        height: 249px;
        background: #f3f3f3;
        border: 1px solid #e6ebf1;
        border-radius: 8px;
        box-sizing: border-box;
        img {
          width: 100%;
          height: 100%;
          border-radius: 8px;
        }
      }
      .list-content {
        padding: 14px 0 6px;
        display: flex;
        .item-tag {
          padding: 0 8px;
          background: #eb61171a;
          border-radius: 4px;
          font-family: PingFangSC-Regular;
          font-weight: 400;
          font-size: 14px;
          color: #eb6117;
          display: flex;
          align-items: center;
          justify-content: center;
          margin-right: 8px;
          flex-shrink: 0;
          &.orange {
            background: #eb61171a;
            color: #eb6117;
          }
          &.green {
            background: #8c1deb1a;
            color: #7413c7;
          }
          &.purple {
            background: #8c1deb1a;
            color: #7413c7;
          }
        }
        .content-title {
          font-family: PingFangSC-Medium;
          font-weight: 500;
          font-size: 16px;
          color: #31323a;
          line-height: 24px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }
    }
  }

  .pagination-box {
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style>
